package state_pattern;

import state_pattern.concrete.DeadState;
import state_pattern.concrete.ReadyState;
import state_pattern.concrete.StandState;

/**
 * @author 申书航
 * @version 1.0
 * 游戏核心类，负责角色之间的战斗逻辑。
 */
public class GameCore {
    /**
     * 进行战斗的方法，创建角色并处理攻击和状态更新。
     */
    public void fight() {
        // 创建角色
        Role role1 = Role.builder().name("吕布").hp(300).attack(100).speed(5).targetPos(20).build();
        Role role2 = Role.builder().name("亚瑟").hp(400).attack(90).speed(4).targetPos(20).build();
        role1.changeState(new ReadyState(role1));
        role2.changeState(new ReadyState(role2));
        // 开始战斗
        boolean isRole1 = true; // 用于轮流攻击
        do {
            if (role1.getState() instanceof StandState && role2.getState() instanceof StandState) {
                if (isRole1) {
                    role1.attack(role2);
                }
                else {
                    role2.attack(role1);
                }
                isRole1 = !isRole1;
            }
            // 更新状态
            System.out.println();
            role1.update();
            role2.update();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } while (!(role1.getState() instanceof DeadState) && !(role2.getState() instanceof DeadState));
    }

    /**
     * 程序的入口点，创建GameCore实例并启动战斗。
     */
    public static void main(String[] args) {
        GameCore gameCore = new GameCore();
        gameCore.fight();
    }
}
